//
// This file is part of cpp-lib, a library of C++ functions and classes.
// Cpp-lib is copyright (C) 2004 and onwards, The CPL Interest Group.
// 
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or (at
// your option) any later version.
// 
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
// General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with this library (see the file COPYING); if not, write to the
// Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
// 02111-1307  USA
//
// $Id$
//

#ifndef CPP_LIB_SERIAL_H
#define CPP_LIB_SERIAL_H

#include <string>
#include <vector>
#include <fstream>
#include <exception>
#include <stdexcept>
#include <memory>

#include "cpp-lib/windows/wrappers.h"


namespace cpl {

namespace serial   {

//
// Configure given device with a config string of the form e.g.
// "baud=9600 parity=o data=8 stop=1"
//
// baud   ... One of the standard RS232 baud rates.
// parity ... o (odd), e (even) or N (none).
// data   ... number of data bits.
// stop   ... number of stop bits.
//
// No whitespace other than between key/value pairs is allowed!
//
// At least the baud and data parameters must be present.
//
// The function opens the device and closes it again immediately.
//
// The device is set up for blocking read and write operations.
//

void configure_device( std::string const& name , std::string const& config ) ;


//
// Serial interface with two separate streams for input and output.  I/O
// is blocking, the read buffer size can be set.
//

class tty {
  
  std::auto_ptr< cpl::detail_::tty_impl > impl ;

  tty const& operator=( tty const& ) ;
  tty                 ( tty const& ) ;

public:

  //
  // Open the serial device name, configure it according to config (see
  // configure_device() above).  Read streambuf buffer size will be n.
  // n >= 1.
  //
  // Throws on any errors.
  //

  tty( 
	std::string const& name   , 
	std::string const& config , 
	int const          n = 1024
  ) ;

  // The input and output streams.
  std::istream in  ;
  std::ostream out ;

} ;


} // namespace serial

} // namespace cpl


#endif // CPP_LIB_SERIAL_H
